Cleaning up (deleting Skip used in Array)

I have a function that is called repeatedly:

on each call it creates a local Skip (holding some external data) that is then put into a global Array.

Later on I delete the Array but am not sure that will also delete the Skips created every time I called the function.

Is this going to cause a memory leak?

 

Here is an example program to illustrate the situation:

//skip in array example
/*
How to delete the Skips?
*/

int i
string data

Array a=create(1,1)
int xIndex=0

void addSkip(string uniqueString)
{
        Skip s=create
        put (s,uniqueString,uniqueString) 
        put (a,s,xIndex++,0) //put the created Skip into the global array 'a'
}


addSkip("some value")
addSkip("some other unique value")

Skip mySkip=create
for (i=0; i<xIndex; i++)
{
        mySkip=(Skip get(a,i,0))
        print "Found within extracted skip #" i ": "
        for data in mySkip do {print data }
        print "\n"
}

delete a,mySkip

//Question: Are all the Skips "s" created in the function call now deleted due to array deletion???

 


strathglass - Fri Nov 27 13:40:59 EST 2015

Re: Cleaning up (deleting Skip used in Array)
morast - Tue Dec 01 04:27:59 EST 2015

Yes, Ithink you have to explicitly delete the Skiplists to avoid memory leak.

In your example you could do it in the existing for loop.

Moreover you should not create/delete mySkip, that's not a new Skip, just a variable pointing to already existing Skips. 

//skip in array example
/*
How to delete the Skips?
*/

int i
string data

Array a=create(1,1)
int xIndex=0

void addSkip(string uniqueString)
{
        Skip s=create
        put (s,uniqueString,uniqueString) 
        put (a,s,xIndex++,0) //put the created Skip into the global array 'a'
}


addSkip("some value")
addSkip("some other unique value")

Skip mySkip
for (i=0; i<xIndex; i++)
{
        mySkip=(Skip get(a,i,0))
        print "Found within extracted skip #" i ": "
        for data in mySkip do {print data }
        print "\n"
        delete mySkip
}

delete a

 

 

 

Re: Cleaning up (deleting Skip used in Array)
Mathias Mamsch - Tue Dec 01 17:18:56 EST 2015

morast - Tue Dec 01 04:27:59 EST 2015

Yes, Ithink you have to explicitly delete the Skiplists to avoid memory leak.

In your example you could do it in the existing for loop.

Moreover you should not create/delete mySkip, that's not a new Skip, just a variable pointing to already existing Skips. 

//skip in array example
/*
How to delete the Skips?
*/

int i
string data

Array a=create(1,1)
int xIndex=0

void addSkip(string uniqueString)
{
        Skip s=create
        put (s,uniqueString,uniqueString) 
        put (a,s,xIndex++,0) //put the created Skip into the global array 'a'
}


addSkip("some value")
addSkip("some other unique value")

Skip mySkip
for (i=0; i<xIndex; i++)
{
        mySkip=(Skip get(a,i,0))
        print "Found within extracted skip #" i ": "
        for data in mySkip do {print data }
        print "\n"
        delete mySkip
}

delete a

 

 

 

I think you should elaborate on what kind of data you want to store in that data structure. Nesting Skips / Arrays is most of the times not a good idea and not efficient. We can probably come up with something easier. Regards, Mathias